﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogicUniversityBiz;
namespace LogicUniversity.clerks
{
    public partial class ProcessDetail : System.Web.UI.Page
    {
        List<RequisitionCustom> reqToAdd;

        protected void Page_PreInit(Object sender, EventArgs e)
        {
            //(Session["Role"]
            if (Session["Role"] as string != "Clerk")
            {
                Response.Redirect("~/Login.aspx");
            }

        }
        public string Dept_Code
        {
            get { return (string)(this.ViewState["dept_Code"]); }
            set { this.ViewState["dept_Code"] = value; }
        }


        public DateTime DateBefore
        {
            get { return (DateTime)this.ViewState["dateBefore"]; }
            set { this.ViewState["dateBefore"] = value; }
        }

        RequisitionController reqController = new RequisitionController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                reqToAdd = new List<RequisitionCustom>();
                //Label1.Text = PreviousPage.RetrievalDateParameter;
                Dept_Code = PreviousPage.DepartmentCode;
                DateBefore = PreviousPage.RetrievalDateParameter;
                var currentRequisition = reqController.getCurrentRequisition(Dept_Code, DateBefore);
                grdRequisitionDetail.DataSource = currentRequisition;
                grdRequisitionDetail.DataBind();
                var outStandingList = reqController.getOutStanding(Dept_Code);

                if (outStandingList == null || outStandingList.Count <= 0)
                {
                    lblOutMsg.Text = "Currently No Outstanding Items.";

                }
                
                    grdOutstanding.DataSource = outStandingList;
                    grdOutstanding.DataBind();
                    reqToAdd = reqController.getRequisitionToAdd(Dept_Code, DateBefore);
                    grdRequisitionToAdd.DataSource = reqToAdd;
                    grdRequisitionToAdd.DataBind();
                    var departmentInfo = reqController.getDepartmentByID(Dept_Code);
                    DepartmentName.Text = departmentInfo.Dept_Name;
                    repName.Text = departmentInfo.Rep_Name;
                    collectionPoint.Text = departmentInfo.Collection_Point;
                    date.Text = DateTime.Now.ToShortDateString();
                    this.ViewState["reqToAdd"] = reqToAdd;
                
            }
        }

        protected void btnAddToRetrieval_Click(object sender, EventArgs e)
        {
            reqToAdd = (List<RequisitionCustom>)(this.ViewState["reqToAdd"]);
            try
            {
                this.getRequestToAdd();
                bool result = reqController.updateAndInsertReterival(Dept_Code, DateBefore, reqToAdd);
                if (result)
                {
                    Response.Redirect("~/clerks/RequisitionList.aspx");
                }
            }
            catch (Exception fe)
            {
                
                return;
            }
        }
        protected void getRequestToAdd()
        {
            List<RequisitionCustom> reqList = new List<RequisitionCustom>();
            reqList = (List<RequisitionCustom>)(this.ViewState["reqToAdd"]);
            int i = 0;
            foreach (GridViewRow r in grdRequisitionToAdd.Rows)
            {
                RequisitionCustom c = new RequisitionCustom();
                //reqList[i].Item_Description = r.Cells[0].Text;
                //reqList[i].Needed =Convert.ToInt16(r.Cells[1].Text);
                //c.Balance = Convert.ToInt16(r.Cells[2].Text);
                try
                {

                    int actQty = Convert.ToInt32(((TextBox)r.FindControl("txtActual")).Text);
                    int balance = Convert.ToInt32((r.Cells[2].Text));
                    int needed = Convert.ToInt16(r.Cells[1].Text);



                    if (actQty < 0)
                    {
                        ((Label)r.FindControl("lblMsg")).Text = "Illegal Quantity";
                        return;
                    }
                    else
                    {
                        if (actQty > balance || actQty > needed)
                        {
                            ((Label)r.FindControl("lblMsg")).Text = "Actual quantity cannot be more than needed or balance";
                            return;
                        }
                    }
                    ((Label)r.FindControl("lblMsg")).Text = null;
                    reqList[i].Actual_Quanty = actQty;
                }
                catch (Exception f)
                {
                    ((Label)r.FindControl("lblMsg")).Text = "Illegal Quantity";
                    throw new Exception("Illegal Quantity");
                }
                //c.Item_Code = r.Cells[4].Text;
                //reqList.Add(c);
                i++;
            }
            this.ViewState["reqToAdd"] = reqList;

        }
    }
}